<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      title="查询库存调整统计分析"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="780px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="12">
          <el-form-item label="过账日期">
            <el-date-picker
              v-model="daterangeRtmJlrq"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="daterange"
              style="width: 240px"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓位代码" prop="ktdCwdms">
            <el-select
              v-model="queryParams.ktdCwdms"
              multiple
              style="width: 240px"
              placeholder="请选择入库仓库"
              clearable
            >
              <el-option
                v-for="item in storageOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="综合查询" prop="itmPmgg">
            <el-input
              v-model="queryParams.itmPmgg"
              placeholder="请输入退料单号"
              clearable
              style="width: 240px"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料代码" prop="ktdWldm">
            <el-input
              v-model="queryParams.ktdWldm"
              style="width: 240px"
              placeholder="请输入物料代码"
              clearable
              size="small"
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['repertory:kcAdjust:export']"
          @click="handleExcel()"
          size="small"
        >
          明细数据
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load"
          :data="rtdRDetail"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="调整单号"
            align="center"
            width="135"
            prop="ktdDjbh"
          />
          <el-table-column
            label="调整仓库"
            class-name="rkck"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ktmCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.ktmCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="ktdWldm"
          />
          <el-table-column
            label="品名规格"
            show-overflow-tooltip
            width="300"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="存放位置"
            align="center"
            width="120"
            prop="ktdCwdm"
          >
            <template #default="scope">
              <dict-tag :options="cWOptions" :value="scope.row.ktdCwdm" />
            </template>
          </el-table-column>
          <el-table-column
            label="库存数量"
            align="center"
            prop="ktdKcsl"
            width="100"
          />
          <el-table-column
            label="实盘数量"
            align="center"
            prop="ktdTzsl"
            width="100"
          />
          <el-table-column
            label="调整差异"
            align="center"
            prop="ktdCysl"
            width="100"
          />
          <el-table-column
            label="调整状态"
            align="center"
            prop="ktdZtbz"
            width="100"
          >
            <template #default="scope">
              <dict-tag :options="lbmTypeOptions" :value="scope.row.ktdZtbz" />
            </template>
          </el-table-column>
          <el-table-column
            label="调整原因"
            align="center"
            prop="ktmTzyy"
            width="100"
          />
          <el-table-column
            label="备注"
            align="center"
            prop="ktdDesc"
            width="100"
          />
          <el-table-column
            label="开单人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ktmJlry"
          />
          <el-table-column
            label="开单日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ktmJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.ktmJlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="批准人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ktmPzry"
          />
          <el-table-column
            label="批准日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ktmPzrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.ktmPzrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="过账人员"
            :show-overflow-tooltip="true"
            width="100"
            align="center"
            prop="ktmGzry"
          />
          <el-table-column
            label="过账日期"
            :show-overflow-tooltip="true"
            width="150"
            align="center"
            prop="ktmGzrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.ktmGzrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table
          border
          stripe
          @scrollBottom="load2"
          :height="tableHChild"
          :data="rtdRWldm"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="rtdWldm"
          />
          <el-table-column
            label="品名规格"
            align="center"
            width="135"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="库存数量"
            align="center"
            prop="rtdSqsl"
            width="100"
          />
          <el-table-column
            label="实盘数量"
            align="center"
            prop="rtdRksl"
            width="100"
          />
          <el-table-column
            label="调整差异"
            align="center"
            prop="rtdRksl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按日期统计" name="third">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load3"
          :data="rtdRDate"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="过账日期"
            align="center"
            width="135"
            prop="rtmGzrq"
          />
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="rtdWldm"
          />
          <el-table-column
            label="品名规格"
            align="center"
            width="135"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            align="center"
            prop="rtdSqsl"
            width="100"
          />
          <el-table-column
            label="入库数量"
            align="center"
            prop="rtdRksl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按仓库统计" name="fifth">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load5"
          :data="rtdRStorage"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column
            label="仓库"
            class-name="rkck"
            :show-overflow-tooltip="true"
            width="140"
            align="center"
            prop="rtdCkdm"
          >
            <template #default="scope">
              <dict-tag
                :options="allStorageOptions"
                :value="scope.row.rtdCkdm"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="物料代码"
            align="center"
            width="135"
            prop="rtdWldm"
          />
          <el-table-column
            label="品名规格"
            align="center"
            width="135"
            prop="itmPmgg"
          />
          <el-table-column
            label="单位"
            :show-overflow-tooltip="true"
            width="60"
            align="center"
            prop="itmDw"
          >
            <template #default="scope">
              <dict-tag :options="unitFlagOptions" :value="scope.row.itmDw" />
            </template>
          </el-table-column>
          <el-table-column
            label="申请数量"
            align="center"
            prop="rtdSqsl"
            width="100"
          />
          <el-table-column
            label="入库数量"
            align="center"
            prop="rtdRksl"
            width="100"
          />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { getAll, getstorageType } from "@/api/repertory/Storage";
import { exportMxData, listMxData } from "@/api/repertory/kcAdjust";

export default {
  name: "KcAdjust",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      serachChildren: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: document.body.clientHeight - 280,
      single: true,
      activeTab: "first",
      // 非多个禁用
      multiple: true,
      // 类型
      lbmTypeOptions: [
        {
          dictValue: "1",
          dictLabel: "新增",
          listClass: "default",
        },
        {
          dictValue: "2",
          dictLabel: "过账成功",
          listClass: "default",
        },
      ],
      rtdRDetail: [],
      rtdRDate: [],
      rtdRStorage: [],
      rtdRWldm: [],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      total3: 0,
      total5: 0,
      //有效存储仓库
      storageOptions: [],

      //所以存储仓库
      allStorageOptions: [],

      //有效存储储位
      cWOptions: [],

      //所有存储储位
      allCwOptions: [],

      // 弹出层标题
      title: "",
      addDate: true,
      // 是否显示弹出层
      open: false,
      // 建立日期时间范围
      daterangeRtmJlrq: [
        new Date(new Date().getFullYear(), new Date().getMonth() - 1),
        this.$moment().endOf("month").toDate(),
      ],
      rkmxList: [],
      rktjList: [],
      qgmxList: [],
      rowData: null,
      qgdDetCountList: [],
      serachAddList: [],
      unitFlagOptions: [],
      selectioned: [],
      type1: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        ktdWldm: null,
        itmPmgg: null,
        ktdCwdms: [],
        // index:0
      },

      // 表单参数
      form: {},
      // 表单校验
      //日期初始化
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    //单位
    this.getDicts("sys_unit").then((response) => {
      this.unitFlagOptions = response.data;
    });
    //存放位置
    getstorageType("0").then((response) => {
      this.cWOptions = response.data;

      for (let item of this.cWOptions) {
        item.dictLabel = item.storageDefaultstorage;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getstorageType("0").then((response) => {
      this.storageOptions = response.data;

      for (let item of this.storageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allCwOptions = response.data;

      for (let item2 of this.allCwOptions) {
        item2.dictLabel = item2.storageDefaultstorage;

        item2.listClass = "default";
        item2.dictValue = item2.storageId + "";
      }
    });

    getAll().then((response) => {
      this.allStorageOptions = response.data;

      for (let item of this.allStorageOptions) {
        item.dictLabel = item.storageName;
        item.listClass = "default";
        item.dictValue = item.storageId + "";
      }
      const data = {
        dictLabel: " [TOTAL] ",
        listClass: "default",
        dictValue: " [TOTAL] ",
      };
      this.allStorageOptions.push(data);
    });
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeRtmJlrq && "" != this.daterangeRtmJlrq) {
        this.queryParams.params["beginKtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endKtmJlrq"] = this.moment(
          this.daterangeRtmJlrq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listMxData(this.queryParams).then((response) => {
        //明细
        this.rtdRDetail = this.rtdRDetail.concat(response.data.list1.rows);
        this.total = response.data.list1.total;
        // //物料
        // this.rtdRWldm = this.rtdRWldm.concat(response.data.wldm.rows)
        // this.total2 = response.data.wldm.total
        // //日期
        // this.rtdRDate = this.rtdRDate.concat(response.data.date.rows)
        // this.total3 = response.data.date.total
        // //仓库
        // this.rtdRStorage = this.rtdRStorage.concat(response.data.stroage.rows)
        // this.total5 = response.data.stroage.total
        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    load3() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total3 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    load5() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total5 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 20;
        this.queryParams.pageNum++;
        this.getList();
      }
      return 0;
    },
    handleSearch() {
      this.openSearch = true;
      this.title = "查询来料进货统计分析";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.rtdRDetail = [];
      // this.rtdRDetail  = [];
      // this.rtdRWldm  = [];
      // this.rtdRDate  = [];
      // this.rtdRStorage  = [];
      this.getList();
      this.openSearch = false;
    },
    //导出Excel
    handleExcel() {
      exportMxData(this.queryParams).then((response) => {
        this.download(response.msg);
        // this.queryParams.index = 0;
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
